var flag = lget("shequn");
/*点击头像触发*/
var span_index;
var arr = [];
document.getElementById('add_img').addEventListener('tap', function () {
    span_index = $(".show_img_sroll span").length;
    //最多发6张
    if (span_index >= 7) {
        mui.alert("最多上传6张图片");
    } else {
        if (mui.os.plus) {
            var a = [{
                title: "拍照"
            }, {
                title: "从手机相册选择"
            }];
            plus.nativeUI.actionSheet({
                title: "添加图片",
                cancel: "取消",
                buttons: a
            }, function (b) { /*actionSheet 按钮点击事件*/
                switch (b.index) {
                    case 0:
                        break;
                    case 1:
                        getImage(span_index);
                        /*拍照*/
                        break;
                    case 2:
                        appendByGallery(span_index);
                        /*打开相册*/
                        break;
                    default:
                        break;
                }
            })
        }
    }
}, false);


// 拍照添加文件
function getImage(span_index) {
    var cmr = plus.camera.getCamera();
    cmr.captureImage(function(p) {
        plus.io.resolveLocalFileSystemURL(p, function(entry) {
            var localurl = entry.toLocalURL(); //把拍照的目录路径，变成本地url路径，例如file:///........之类的。
            compressImage(localurl,span_index);
        });
    }, function(error) {
        //alert("Capture image failed: " + error.message);
    });
};

// 从相册添加文件
function appendByGallery(span_index) {
    plus.gallery.pick(function(path) {
        compressImage(path,span_index); //处理图片的地方
    });
};

//上传头像图片
//			var arr = [];
//function uploadHead(imgPath, span_index) {
//				//console.log("imgPath = " + imgPath);
////				mainImage.src = imgPath;
////				mainImage.style.width = "60px";
////				mainImage.style.height = "60px";
//
//				var image = new Image();
//				image.src = imgPath;
//				image.onload = function() {
//					var imgData = getBase64Image(image);
//					/* 图片显示 */

//					/*在这里调用上传接口*/
//					mui.ajax(server_addr + sendImg, {
//						data: {
//							name: imgData,
//							flag: flag
//						},
//						dataType: 'text',
//						type: 'post',
//						timeout: 10000,
//						success: function (data) {
//							//console.log("成功后" + JSON.stringify(data));
//							arr[span_index - 1] = data;
//						},
//						error: function (xhr, type, errorThrown) {
//							mui.toast('网络异常，请稍后再试！');
//							//console.log("失败");
//						}
//					});
//
//				}
//    plus.nativeUI.showWaiting();
//}

//压缩图片
function compressImage(imgsrc,span_index) {
    plus.nativeUI.showWaiting("等待中...");
    $("#add_img").before('<span><img src="" alt=""></span>');
    $(".show_img_sroll span:nth-child(" + span_index + ")").find("img").attr("src", imgsrc);
    //if (span_index == 6) {
    //    $("#add_img").hide();
    //}
    plus.zip.compressImage({
            src: imgsrc,
            dst: "_documents/a.jpg",
            overwrite: true,
            quality: 20
        },
        function(e) {
            //alert("Compress success!");
            //var pic = document.getElementById("picture");
            var pic = $(".show_img_sroll span:nth-child(" + span_index + ")").find("img");
            //			alert(e.target)
            pic.src = e.target;
            createUpload(e.target,span_index);
        },
        function(error) {
            //alert("Compress error!" + JSON.stringify(error));
        });
}

function createUpload(path,span_index) {
    var task = plus.uploader.createUpload(sendImg,      //创建上传任务
        {method: "POST"},
        function (t, status) {
            // 上传完成
            //alert("完成" + JSON.stringify(t));
//				alert(status);
//            plus.nativeUI.closeWaiting();
            if (status == 200) {
                var json_text = t.responseText;
                var JSON_Ob = eval('(' + json_text + ')');
                arr[span_index-1] = JSON_Ob.id;
                //JSON_Ob.id
                //alert("Upload success: " + t.url);
//				mui.toast("上传成功");
                plus.nativeUI.closeWaiting();
            } else {
                //alert("Upload failed: " + status);
                plus.nativeUI.closeWaiting();
            }
        }
    );
    task.addData('flag', flag);
    task.addFile(path, {key: "image"});
    //task.addEventListener( "statechanged", onStateChanged, false );
    task.start();
}
